package main

import (
  "sdl"
  "sdl/ttf"
  "sdl/mixer"
  "flag"
  "fmt"
  "os"
  "log"
)

var screenWidth  *int  = flag.Int("w", 640, "screen width")
var screenHeight *int  = flag.Int("h", 480, "screen height")
var fullscreen   *bool = flag.Bool("f", false, "run in full screen")
var help         *bool = flag.Bool("help", false, "show help")

var input *Input

func initSdl() *sdl.Surface {
  if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
    panic(sdl.GetError())
  }

  if ttf.Init() != 0 {
    panic(sdl.GetError())
  }

  if mixer.OpenAudio(mixer.DEFAULT_FREQUENCY, mixer.DEFAULT_FORMAT,
      mixer.DEFAULT_CHANNELS, 4096) != 0 {
    panic(sdl.GetError())
  }

  var screen = sdl.SetVideoMode(640, 480, 32, 0)
  if screen == nil {
    panic(sdl.GetError())
  }

  // Set VM Caption:
  sdl.WM_SetCaption("Dice Battle!", "")
  
  // Set WM Icon:
  wmIcon := sdl.Load("resources/logo/dice-battle.png")
  if wmIcon == nil {
    panic(sdl.GetError())
  }
  sdl.WM_SetIcon(wmIcon, nil)
  wmIcon.Free()
  

  font := ttf.OpenFont("resources/fonts/LiberationSans-Regular.ttf", 16)
  cont := NewContainer(100, 100, 150, 250)
  UIInit(cont, font)
  cont.add(NewLabel(0, 0, 100, 25, "Server"))
  buttons := NewButton(0, 0, 100, 30, "Start server", runServer)
  cont.add(buttons)
  cont.add(NewLabel(0, 0, 100, 25, "Client"))
  cont.add(NewLabel(0, 0, 100, 25, "IP Address"))
  input = NewInput(0, 0, 100, 30)
  cont.add(input)
  buttonc := NewButton(0, 0, 100, 30, "Connect to client", runClient)
  cont.add(buttonc)

  
  sdl.EnableUNICODE(1)
  
  fmt.Printf("Okay... let's see. \nContainer: %p, \nButton: %p, \nInput: %p\n", cont, buttons, input)
  
  return screen
}


func runClient(win Window, x, y int) {
  log.Stderr("Client started")
  
  ipAddress := input.getText()
  var out chan string
  if ipAddress == "" {
    _, out = new(NetworkClient).initialize(flag.Arg(1))
  } else {
    _, out = new(NetworkClient).initialize("127.0.0.1")
  }
  
  for {
    out <- "PINGPONGBOLLARNA ÄR SLUT!"
  }
}

func runServer(win Window, x, y int) {
  log.Stderr("Server started")
  clients := new(NetworkServer).initialize()
  for{
    client := <- clients
    go handleClient(client)
  }
}


func main() {
  flag.Parse()

  screen  := initSdl()
  running := true

  for running {
    //log.Stderr("Runnign...")
    e := &sdl.Event{}

    for e.Poll() {
      
      // Handle if not consumed by UI:
      if !DoInput(e) {
        switch e.Type {
          case sdl.QUIT:
            running = false
            break
          default:
            //log.Stderr(e.Type)
            break
        }
      }
    }
    //log.Stderr("tetasdfasdf")
    Draw(screen)
    screen.Flip()
    screen.FillRect(&sdl.Rect{0, 0, uint16(screen.W), uint16(screen.H)}, 0x000000)
  }

  //font.Close()
  ttf.Quit()  
  sdl.Quit()

}

func handleClient(client Client){
  in := client.in
  for{ 
    s := <- in
    if s != "PINGPONGBOLLARNA ÄR SLUT!" {
      os.Exit(1)
    }
  }
}

type Client struct{
  in chan string /* received messages will be forwarded to this channel */
  out chan string /* messages sent to this channel will be forwarded to the client */
}
